package com.ruoyi.web.controller.ydz;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.domain.Enterprise;
import com.ruoyi.web.service.IEnterpriseService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 药企管理Controller
 * 
 * @author drx
 * @date 2024-03-25
 */
@Controller
@RequestMapping({"/ydz/enterprise","/api/enterprise"})
@Api(tags = "药企管理")
public class EnterpriseController extends BaseController
{
    private String prefix = "ydz/enterprise";

    @Autowired
    private IEnterpriseService enterpriseService;

    @RequiresPermissions("ydz:enterprise:view")
    @GetMapping()
    public String enterprise()
    {
        return prefix + "/enterprise";
    }

    /**
     * 查询药企管理列表
     */
    @RequiresPermissions("ydz:enterprise:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("查询药企管理列表")
    public TableDataInfo list(Enterprise enterprise)
    {
        startPage();
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 查询药企管理列表
     */
    @PostMapping("/getEnterpriseList")
    @ResponseBody
    @ApiOperation("查询药企管理列表")
    public TableDataInfo getEnterpriseList(@RequestBody Enterprise enterprise)
    {
        startPage();
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        return getDataTable(list);
    }

    /**
     * 导出药企管理列表
     */
    @RequiresPermissions("ydz:enterprise:export")
    @Log(title = "药企管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    @ApiOperation("导出药企管理列表")
    public AjaxResult export(Enterprise enterprise)
    {
        List<Enterprise> list = enterpriseService.selectEnterpriseList(enterprise);
        ExcelUtil<Enterprise> util = new ExcelUtil<Enterprise>(Enterprise.class);
        return util.exportExcel(list, "药企管理数据");
    }

    /**
     * 新增药企管理
     */
    @GetMapping("/add")
    @ApiOperation("新增药企管理")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存药企管理
     */
    @ApiOperation("新增保存药企管理")
    @RequiresPermissions("ydz:enterprise:add")
    @Log(title = "药企管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Enterprise enterprise)
    {
        return toAjax(enterpriseService.insertEnterprise(enterprise));
    }

    /**
     * 修改药企管理
     */
    @ApiOperation("修改药企管理")
    @RequiresPermissions("ydz:enterprise:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Enterprise enterprise = enterpriseService.selectEnterpriseById(id);
        mmap.put("enterprise", enterprise);
        return prefix + "/edit";
    }

    /**
     * 修改保存药企管理
     */
    @ApiOperation("修改保存药企管理")
    @RequiresPermissions("ydz:enterprise:edit")
    @Log(title = "药企管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Enterprise enterprise)
    {
        return toAjax(enterpriseService.updateEnterprise(enterprise));
    }

    /**
     * 删除药企管理
     */
    @ApiOperation("删除药企管理")
    @RequiresPermissions("ydz:enterprise:remove")
    @Log(title = "药企管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(enterpriseService.deleteEnterpriseByIds(ids));
    }

    /**
     * 获取新闻详情-前端调用
     */
    @ApiOperation("获取药企详情")
    @GetMapping("/get/{id}")
    @ResponseBody
    public AjaxResult get(@PathVariable("id") Long id)
    {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            Enterprise enterprise = enterpriseService.selectEnterpriseById(id);
            ajaxResult.put(AjaxResult.DATA_TAG,enterprise);
            ajaxResult.put(AjaxResult.CODE_TAG, Constants.SUCCESS);
            ajaxResult.put(AjaxResult.MSG_TAG,"查询成功！");
        } catch (Exception e) {
            ajaxResult.put(AjaxResult.CODE_TAG, Constants.FAIL);
            ajaxResult.put(AjaxResult.MSG_TAG,"药企查询失败，请联系管理员！");
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
